package com.futureCloudMOM.core.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.futureCloudMOM.common.enums.ProdBillStatus;
import com.futureCloudMOM.common.exception.base.BaseException;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.ProduceTaskBill;
import com.futureCloudMOM.core.domain.ProduceTaskBillRoutingProcess;
import com.futureCloudMOM.core.domain.ProduceTaskBillSub;
import com.futureCloudMOM.core.mapper.ProduceTaskBillMapper;
import com.futureCloudMOM.core.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 【Produce_TaskBill】的数据库操作Service实现
 *
 * @author Administrator
 * @since 2023-06-06 16:02:57
 */
@Slf4j
@AllArgsConstructor
@Service
public class ProduceTaskBillServiceImpl extends BaseServiceImpl<ProduceTaskBillMapper, ProduceTaskBill> implements ProduceTaskBillService {

    // 订单工艺工序
    private final ProduceTaskBillRoutingProcessService produceTaskBillRoutingProcessService;
    // 订单子表
    private final ProduceTaskBillSubService produceTaskBillSubService;

    @Transactional
    @Override
    public void approve(Long id) {
        ProduceTaskBill taskBill = getById(id);
        if (ObjectUtils.isEmpty(taskBill)) {
            throw new BaseException("未查询到生产订单");
        }else if(taskBill.getStatus() != 0 ){
            throw new BaseException("订单状态是待审核的订单，才可以进行审核");
        }else
        if (ObjectUtils.isEmpty(taskBill.getRoutingId())) {
            throw new BaseException("请先维护工艺信息");
        }else
        if (ObjectUtils.isEmpty(taskBill.getBomId())) {
            throw new BaseException("请先维护BOM信息");
        }
        // 按照绑定的工艺路线 复制基础数据中的工艺工序 工艺参数 工艺用料信息
        produceTaskBillRoutingProcessService.create(id, taskBill.getRoutingId());

        //produceTaskBillMaterialService.create(id, bomId);
        // 订单状态修改为已审核
        taskBill.setStatus(1L);
        updateById(taskBill);
    }

    @Transactional
    @Override
    public void deApprove(Long id) {
        ProduceTaskBill taskBill = getById(id);
        if (ObjectUtils.isEmpty(taskBill)) {
            throw new BaseException("未查询到订单，或已删除，请刷新页面");
        }else if(taskBill.getStatus() != 1){
            throw new BaseException("订单状态是已审核的订单，才可以进行反审核");
        }
        //todo 硬删除订单关联表的数据
        produceTaskBillRoutingProcessService.deleteByBillId(id);
        //produceTaskBillMaterialService.deleteByBillId(id);
        // 订单状态修改未待审核
        taskBill.setStatus(0L);
        updateById(taskBill);
    }

    @Transactional
    @Override
    public void release(Long id) {
        ProduceTaskBill taskBill = getById(id);
        if (ObjectUtils.isEmpty(taskBill)) {
            throw new BaseException("未查询到生产订单");
        }else if(taskBill.getStatus() != 1){
            throw new BaseException("订单状态是已审核的订单，才可以进行下达");
        }
        //todo 生成订单子表
        produceTaskBillSubService.create(taskBill);
        // 状态改为已下达
        taskBill.setStatus(2L);
        updateById(taskBill);
    }

    @Transactional
    @Override
    public void withdrawalOfRelease(Long id) {
        ProduceTaskBill taskBill = getById(id);
        if (ObjectUtils.isEmpty(taskBill)) {
            throw new BaseException("未查询到生产订单");
        }else if(taskBill.getStatus() != 2){
            throw new BaseException("订单状态为已下达的订单，才可以进行反下达");
        }
        //if (produceDispatchService.hasRunningDispatch(id)) {
        //    throw new BaseException("生产订单有已下达的工单，无法反下达");
        //}
        produceTaskBillSubService.deleteByBillId(id);
        // 状态改为已审核
        taskBill.setStatus(1L);
        updateById(taskBill);
    }

    @Override
    public void produceClose(Long id) {
        ProduceTaskBill taskBill = getById(id);
        if(taskBill.getStatus() != 0){
            throw new BaseException("订单状态是待审核的订单，才可以关闭");
        }
        // 订单状态改为关闭
        taskBill.setStatus(3L);
        updateById(taskBill);
    }

    @Override
    public void produceUnclose(Long id) {
        ProduceTaskBill taskBill = getById(id);
        if(taskBill.getStatus() != 3){
            throw new BaseException("订单状态是已关闭的订单，才可以反关闭");
        }
        // 订单状态改为待审核
        taskBill.setStatus(0L);
        updateById(taskBill);
    }

    @Override
    public List<ProduceTaskBillRoutingProcess> getProcessListByTaskBillId(Long id) {
        return produceTaskBillRoutingProcessService.selectListByBillId(id);
    }

    @Override
    public List<ProduceTaskBillSub> getTaskBillSubListByTaskBillId(Long id) {
        return produceTaskBillSubService.list(new LambdaQueryWrapper<ProduceTaskBillSub>()
        .eq(ProduceTaskBillSub::getTaskBillId,id));
    }

}
